﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using Commons.Entity;

namespace Commons.Web
{
    /// <summary>
    /// 公用Controller
    /// </summary>
    public class WebCommonController : Controller
    {
        #region 上传相关
        /// <summary>
        /// 上传对象GET请求
        /// </summary>
        /// <param name="ParentImgSrc">父页面IMG名称</param>
        /// <param name="ParentImgInput">父页面Input名称</param>
        /// <param name="ImageWidth">图像宽</param>
        /// <param name="ImageHeight">图像高</param>
        /// <param name="FileName">文件名</param>
        /// <param name="IsCompletePath">是否完整路径</param>
        /// <param name="UploadPath">上传的二级目录</param>
        /// <returns></returns>
        public ActionResult Upload(string ParentImgSrc
            , string ParentImgInput
            , string ImageWidth
            , string ImageHeight
            , string FileName
            , int? IsCompletePath
            , string UploadPath)
        {
            ViewBag.ParentImgSrc = string.IsNullOrWhiteSpace(ParentImgSrc) ? "headpic" : ParentImgSrc;
            ViewBag.ParentImgInput = string.IsNullOrWhiteSpace(ParentImgInput) ? "UserImg" : ParentImgInput;
            ViewBag.ImageWidth = ImageWidth;
            ViewBag.ImageHeight = ImageHeight;
            ViewBag.FileName = string.IsNullOrWhiteSpace(FileName) ? "FileName" : FileName;
            ViewBag.IsCompletePath = IsCompletePath.HasValue ? IsCompletePath.Value : 1;
            ViewBag.UploadPath = UploadPath;
            return View();
        }

        /// <summary>
        /// 上传对象POST
        /// </summary>
        /// <param name="formcollection"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Upload(FormCollection formcollection)
        {
            FileUpload _FileUploadWebServices = new FileUpload();
            //父页面Img标签名称
            string parentImgSrc = Request.Form["ParentImgSrc"];
            //父页面Input标签名称
            string parentImgInput = Request.Form["ParentImgInput"];
            int isCompletePath;
            int.TryParse(Request.Form["IsCompletePath"], out isCompletePath);
            int width, height;
            int.TryParse(Request.Form["ImageWidth"], out width);
            int.TryParse(Request.Form["ImageHeight"], out height);
            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase file = Request.Files[0];
                VMessage vm = new VMessage();
                try
                {
                    if (width == 0 && height == 0)
                        vm = _FileUploadWebServices.Image(UpLoadType.FileUrl
                            , file
                            , true
                            , Request.Form["UploadPath"]);
                    else
                        vm = _FileUploadWebServices.Image(UpLoadType.FileUrl
                            , file
                            , new ImageSize(width, height)
                            , true
                            , Request.Form["UploadPath"]);
                }
                catch
                {
                    ViewBag.Msg = "上传失败";
                }

                if (vm.IsComplete)
                {
                    ViewBag.Url = isCompletePath == 1 ? vm[1].Replace("\"", "") : vm[0].Replace("\"", "");
                    ViewBag.ParentImgSrc = Request.Form["ParentImgSrc"];  //父页面Img标签名称
                    ViewBag.ParentImgInput = Request.Form["ParentImgInput"]; //父页面Input标签名称
                    ViewBag.FileName = Request.Form["FileName"]; //文件的原始名称
                    ViewBag.FileNameValue = file.FileName;
                    ViewBag.ImageWidth = width;
                    ViewBag.ImageHeight = height;
                    ViewBag.IsCompletePath = isCompletePath;
                    ViewBag.UploadPath = Request.Form["UploadPath"];
                    ViewBag.Msg = "上传成功";
                }
                else
                {
                    ViewBag.Msg = "上传失败";
                }
            }
            return View();
        }
        #endregion
    }
}
